
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.document.operation.impl {
import org.goverla.collections.ArrayList;

import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.BufferedDocInitialization;

/**
 * A builder for {@link BufferedDocInitialization}s.
 * 
 * Use {@link DocInitializationBuffer} instead if you need an implementation of
 * the interface
 * {@link org.waveprotocol.wave.model.document.operation.EvaluatingDocOpCursor}.
 */
public class DocInitializationBuilder {
  private static var EMPTY_ARRAY:Array=
      new DocInitializationComponent[0];

  private var accu:ArrayList= new ArrayList();

  /**
   * Constructs an operation from this builder's state.
   * 
   * Behaviour is undefined if this builder is used after calling this method.
   */
  public final function build():BufferedDocInitialization {
    // TODO: This should not need a call to asInitialization().
    return DocumentOperationUtil.asInitialization(BufferedDocOpImpl.create(accu)) as BufferedDocInitialization;
  }

  /** @see #build() */
  // This is dangerous; we currently use it for ill-formedness-detection
  // tests, and may use it for efficiency in other places in the future.
  public final function buildUnchecked():BufferedDocInitialization {
    // TODO: This should not need a call to asInitialization().
    return DocumentOperationUtil.asInitialization(BufferedDocOpImpl.createUnchecked(accu)) as BufferedDocInitialization;
  }

  public final function annotationBoundary(map:AnnotationBoundaryMap):DocInitializationBuilder {
    accu.addItem(new AnnotationBoundary(map));
    return this;
  }
  public final function characters(s:String):DocInitializationBuilder {
    accu.addItem(new Characters(s));
    return this;
  }
  public final function elementEnd():DocInitializationBuilder {
    accu.addItem(ElementEnd.INSTANCE);
    return this;
  }
  public final function elementStart(type:String, attrs:Attributes):DocInitializationBuilder {
    accu.addItem(new ElementStart(type, attrs));
    return this;
  }
}
}